package com.atguigu.nio.channel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class NIOFileChannel04 {
    public static void main(String[] args) {

        try (//创建相关流
             FileInputStream fileInputStream = new FileInputStream("d:\\a.jpg");
             FileOutputStream fileOutputStream = new FileOutputStream("d:\\a2.jpg");
             //获取各个流对应的filechannel
             FileChannel fileInputStreamChannel = fileInputStream.getChannel();
             FileChannel fileOutputStreamChannel = fileOutputStream.getChannel()) {

//        //使用transferTo完成拷贝
//        fileInputStreamChannel.transferTo(0, fileInputStreamChannel.size(), fileOutputStreamChannel);
            // 或者使用transferForm完成拷贝
            fileOutputStreamChannel.transferFrom(fileInputStreamChannel, 0, fileInputStreamChannel.size());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
